package controller;

import java.io.IOException;
import java.util.ArrayList;

import DAO.DietDAO;
import DAO.DishDAO;
import DAO.MenuDAO;
import DAO.PlanDAO;
import DAO.RecipeDAO;
import DTO.DietDTO;
import DTO.DishDTO;
import DTO.MenuDTO;
import DTO.PlanDTO;
import DTO.RecipeDTO;
import XMLEntities.DietListXM;
import XMLEntities.DietXM;
import XMLEntities.DishListXM;
import XMLEntities.DishXM;
import XMLEntities.MenuListXM;
import XMLEntities.MenuXM;
import XMLEntities.PlanInMealListXM;
import XMLEntities.RecipeDetailListXM;
import XMLEntities.RecipeDetailXM;
import XMLEntities.RecipeListXM;
import XMLEntities.RecipeXM;
import XMLUtilities.JAXBUtilities;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GenerateServlet
 */
public class GenerateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GenerateServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("ADMIN_USERNAME") != null) {
			try {
				GenerateMenu();
				GenerateDiet();
				GenerateRecipe();
				GeneratePlan();
				GenerateDish();
				request.setAttribute("GENERATE_STATUS", true);
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("GENERATE_STATUS", false);
			}
			RequestDispatcher rd = request
					.getRequestDispatcher("admin/admin-home.jsp");
			rd.forward(request, response);

		} else {
			RequestDispatcher rd = request
					.getRequestDispatcher("admin/admin-login.html");
			rd.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	private void GenerateMenu() {
		MenuDAO dao = new MenuDAO();
		ArrayList<MenuDTO> menuDTOlist = new ArrayList<MenuDTO>();
		menuDTOlist = dao.select();

		ArrayList<MenuXM> menuXMlist = new ArrayList<MenuXM>();

		for (MenuDTO menuDTO : menuDTOlist) {
			MenuXM menuXM = new MenuXM();
			menuXM.setId(menuDTO.getId());
			menuXM.setName(menuDTO.getName());
			menuXM.setDietId(menuDTO.getDiet().getId());
			menuXM.setDescription(menuDTO.getDescription());
			menuXM.setImgPath(menuDTO.getImgPath());
			menuXM.setNumberday(menuDTO.getNumberday());
			menuXM.setNumbermeal(menuDTO.getNumbermeal());
			menuXMlist.add(menuXM);
		}
		//
		MenuListXM list = new MenuListXM(menuXMlist);
		String realPath = getServletContext().getRealPath("/");
		String filePath = "WEB-INF\\xml\\menu.xml";
		System.out.println(realPath + filePath);
		JAXBUtilities.marshall_toFile(realPath + filePath, list);
	}

	private void GenerateRecipe() {
		RecipeDAO recipeDAO = new RecipeDAO();
		ArrayList<RecipeDTO> recipeDTOList = new ArrayList<RecipeDTO>();
		recipeDTOList = recipeDAO.select();

		RecipeListXM recipeListXM = new RecipeListXM();
		RecipeDetailListXM recipeDetailListXM = new RecipeDetailListXM();
		for (RecipeDTO recipeDTO : recipeDTOList) {
			RecipeXM recipeXM = new RecipeXM();
			recipeXM.setId(recipeDTO.getId());
			recipeXM.setName(recipeDTO.getName());
			recipeListXM.getRecipeList().add(recipeXM);

			RecipeDetailXM recipeDetailXM = new RecipeDetailXM();
			recipeDetailXM.setId(recipeDTO.getId());
			DishDAO dishDAO = new DishDAO();
			ArrayList<DishDTO> dishInRecipe = dishDAO.getDishInRecipe(recipeDTO
					.getId());
			if (dishInRecipe != null) {
				for (DishDTO dishDTO : dishInRecipe) {
					recipeDetailXM.getDishList().getDishList()
							.add(dishDAO.convertDishtoDishXM(dishDTO));
				}
			}
			recipeDetailListXM.getRecipeDetailList().add(recipeDetailXM);
		}
		// generate RecipeList
		String realPath = getServletContext().getRealPath("/");
		String filePath = "WEB-INF\\xml\\recipe.xml";
		System.out.println(realPath + filePath);
		JAXBUtilities.marshall_toFile(realPath + filePath, recipeListXM);

		// generate RecipeDetail
		filePath = "WEB-INF\\xml\\recipe-detail.xml";
		System.out.println(realPath + filePath);
		JAXBUtilities.marshall_toFile(realPath + filePath, recipeDetailListXM);
	}

	private void GenerateDiet() {
		DietDAO dietDAO = new DietDAO();
		ArrayList<DietDTO> dietList = new ArrayList<DietDTO>();
		dietList = dietDAO.select();

		DietListXM dietListXM = new DietListXM();
		for (DietDTO dietDTO : dietList) {
			DietXM dietXM = new DietXM(dietDTO.getId(), dietDTO.getName());
			dietListXM.getDietList().add(dietXM);
		}

		String realPath = getServletContext().getRealPath("/");
		String filePath = "WEB-INF\\xml\\diet.xml";
		System.out.println(realPath + filePath);
		JAXBUtilities.marshall_toFile(realPath + filePath, dietListXM);
	}

	private void GeneratePlan() {
		PlanDAO planDAO = new PlanDAO();
		ArrayList<PlanDTO> planList = new ArrayList<PlanDTO>();
		planList = planDAO.select();

		PlanInMealListXM planInMealListXM = new PlanInMealListXM();
		planInMealListXM = planDAO.convertPlansToPlanInMeals(planList);

		String realPath = getServletContext().getRealPath("/");
		String filePath = "WEB-INF\\xml\\plan.xml";
		System.out.println(realPath + filePath);
		JAXBUtilities.marshall_toFile(realPath + filePath, planInMealListXM);
	}

	private void GenerateDish() {
		DishDAO dishDAO = new DishDAO();
		ArrayList<DishDTO> dishList = new ArrayList<DishDTO>();
		dishList = dishDAO.select();

		DishListXM dishListXM = new DishListXM();
		for (DishDTO dish : dishList) {
			DishXM dishXM = dishDAO.convertDishtoDishXM(dish);
			dishListXM.getDishList().add(dishXM);
		}

		String realPath = getServletContext().getRealPath("/");
		String filePath = "WEB-INF\\xml\\dish.xml";
		System.out.println(realPath + filePath);
		JAXBUtilities.marshall_toFile(realPath + filePath, dishListXM);
	}
}
